﻿/*****************************************************************
 * Copyright (C) Newegg Corporation. All rights reserved.
 * 
 * Author       :  Bill Li (Bill.S.Li@newegg.com)
 * Create Date  :  
 * Usage        :  
 *
 * RevisionHistory
 * Date         Author               Description
 * 
*****************************************************************/
using System;
using System.Collections.Generic;

namespace TemplateEngine
{
    internal static class TranslateManager
    {
        private static Dictionary<string, Type> s_dict;
        private static object s_locker;

        static TranslateManager()
        {
            s_dict = new Dictionary<string, Type>();
            s_locker = new object();
        }

        /// <summary>
        /// 获取翻译命令对象
        /// </summary>
        /// <param name="translateCommmand">翻译命令</param>
        /// <returns>ITranslate对象</returns>
        internal static ITranslate GetTranslate(string translateCommmand)
        {
            if (!s_dict.ContainsKey(translateCommmand))
            {
                lock (s_locker)
                {
                    if (!s_dict.ContainsKey(translateCommmand))
                    {
                        string type_str = string.Empty;
                        for (int i = 0; i < translateCommmand.Length; i++)
                        {
                            if (i == 0)
                            {
                                type_str += translateCommmand[i].ToString().ToUpper();
                            }
                            else
                            {
                                type_str += translateCommmand[i];
                            }
                        }

                        Type type = Type.GetType(string.Format("TemplateEngine.TranslateCommand.{0}Translate", type_str));



                        s_dict.Add(translateCommmand, type);
                    }
                }
            }

            var translate = Activator.CreateInstance(s_dict[translateCommmand]) as ITranslate;
            return translate;
        }
    }
}
